<?php

class Leaderboard {
	
	public $id;	
	public $igraci;	
		
	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
	}
	
	// functions
	
	public function getPlayersList() {
		$q = "SELECT id FROM igrac";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Nema igraca!");
		while ($n = mysql_fetch_array($r)) {
			$this->igraci[] = new Player($n['id']);
		}
	}
	
	public function fillPlayersStats() {
		foreach($this->igraci as $igrac) {
			$igrac->getAllStats();
			$igrac->calculateTotalStats();
		}
	}
	
	public function sortPlayers($param, $type = "asc") {
		$num = count($this->igraci);
		
		$k = true;
		while ($k) {
			$k = false;
			for ($i=0; $i<$num-1; $i++) {
				if ($this->igraci[$i]->totalStats->$param < $this->igraci[$i+1]->totalStats->$param) {
					$k = true;
					list($this->igraci[$i], $this->igraci[$i+1]) = array($this->igraci[$i+1], $this->igraci[$i]);
				}
			}
		}
		
		if ($type == "desc") {
			rsort($this->igraci);
		}
	}
				
} // end of class Leaderboard


?>